<!--sidebar-menu-->
<div id="sidebar" class="clearfix"><!-- <a href="#" class="visible-phone"><i class="icon icon-home"></i> Dashboard</a> -->
  <ul style="display: block;">
  {% include "common/menu.html" %}
  </ul>
</div>
<!--end sidebar-menu-->

<div id="content">
  
    <div class="container-fluid" style="padding: 0;">
      <div class="widget-box tab-tabs" >
          <div class="widget-title">
            <ul class="nav nav-tabs">
             {% include "common/tab.html" %}
            </ul>
          </div>
          <div class="widget-content tab-content">
              <!--{{announcementData|json_encode}}-->
            <div id="tab2" class="tab-pane active">
              <div class="container-fluid">
                <div class="row-fluid" style="margin: 20px 0;overflow: hidden;font-size: 20px;">
                    {% if announcementData['status'] ==1 %}
                    <span class="pull-left">轮播持续时长：</span>
                    <span class="pull-left">{{announcementData['runHours']}}</span>
                    <span class="pull-left">小时</span>
                    <span class="pull-left" style="margin-left: 20px;">时间间隔：</span>
                    <span class="pull-left">{{announcementData['seconds']}}</span>
                    <span class="pull-left">秒</span>
                    <span class="pull-left" style="margin-left: 20px;">已运行：</span>
                    <span class="pull-left">{{announcementData['hasRunTimes']}}</span>
                    <span class="pull-left" style="margin-left: 20px;">剩余：</span>
                    <span class="pull-left">{{announcementData['leftTimes']}}</span>
                    <button onclick="closeAnnouncement()"  class="btn btn-purple pull-left" style="margin-left: 40px;">关闭轮播&nbsp;</button>
                    {% else %}
                    <span class="pull-left">轮播持续时长：</span>
                    <input id="LCount" type="text" placeholder="只能输入数字" class=" search pull-left " maxlength="8" onkeydown="onlyNum()" style="ime-mode:Disabled;width: 100px;">
                    <span class="pull-left" style="font-size: 12px;margin-top: 5px;">(单位：小时)</span>
                    <span class="pull-left" style="margin-left: 40px;">轮播间隔：</span>
                    <input id="LRate" type="text" placeholder="只能输入数字" class=" search pull-left " maxlength="8" onkeydown="onlyNum()" style="ime-mode:Disabled">
                    <span class="pull-left" style="margin-top: 5px;font-size: 12px;">(单位：秒)</span>
                    <button onclick="sendAnnouncement()"  class="btn btn-purple pull-left" style="margin-left: 20px;">开启轮播&nbsp;</button>
                    <label class="labelP pull-left" style="margin-top: 10px;margin-left: 10px;cursor: default;"></label>
                    {% endif %}
                    <button id="btnSearch" type="button" onclick="addNoticeDialog(0)" class="btn btn-purple pull-right">添加&nbsp;</button>
                </div>

                <div class="row-fluid">
                  <table id="tableList" class="table with-check table-purple">
                    <thead>
                      <tr>
                        <th>序号</th>
                        <th>公告内容</th>
                        <th>链接</th>
                        <th>次数</th>
                        <th>操作</th>
                      </tr>
                    </thead>
                    <tbody>
                      
                    </tbody>
                  </table>
                </div>
                  <!--{% include "common/pagination.html" %}-->

                </div>
              </div>
            </div>

         </div>
        </div>
    
    </div>

</div>
<div id="addNoticeDialog" style="display:none"> <!--class="ui-dialog"-->
    <form id="addNoticeDialogForm">
        <div>
            <span>内容:</span>
            <textarea id="reason" rows="5" style="width: 98%;margin-top: 10px;"></textarea>
        </div>
        <div>
            <span>链接:</span>
            <textarea id="lianjei" rows="3" style="width: 98%;margin-top: 10px;"></textarea>
        </div>
        <div>
            <span>次数:</span>
            <textarea id="times" rows="1" style="width: 98%;margin-top: 10px;" onkeydown="onlyNum()" placeholder="只能输入数字"></textarea>
        </div>
    </form>
</div>
<div id="delrecommendDialog" style="display:none">
    <div style="height: 115px;line-height: 115px;text-align: center;font-size: 16px;">确定删除这条数据？</div>
</div>
<script type="text/javascript">

    $(function(){
        noticeData();
    });

    function noticeData(index){
        var form=global.doc('formSearch');
        var data={};
        data.page=index||1;
        data.numPerPage=global.pageSize;
        $.ajax({
            type:"POST",
            data:data,
            url:'/ajax/getAnnouncement',
            dataType:'json',
            success:function(res){
                var data=res.data;
                var tableList=document.getElementById("tableList");
                var tbody= tableList.getElementsByTagName('tbody')[0];
                dataTable.clearData(tbody);

//                var count=document.getElementById('count');
//                count.innerHTML=data.count;

                for(var i=0;i<data.list.length;i++){
                    addRow(tbody,data.list[i]);
                }
//                if(!index||index==0){
//                    utilsPagination("#pagination",data.count,global.pageSize,function(page_index,jq){
//                        page_index+=1;
//                        noticeData(page_index);
//                    });
//                }
            }

        });
    }

    function addRow(table,data){

        var row=table.insertRow();
        var cell=row.insertCell(0);
        var cell1=row.insertCell(1);
        var cell2=row.insertCell(2);
        var cell3=row.insertCell(3);
        var cell4=row.insertCell(4);


        cell.style.cssText='text-align:center;width: 8%;';
        cell.innerHTML=data.id;

        cell1.style.cssText='text-align:left;width: 35%;';
        cell1.innerHTML=data.content;
        cell2.style.cssText='text-align:center;width: 35%;';
        cell2.innerHTML=data.url;
        cell3.style.cssText='text-align:center;width: 7%;';
        cell3.innerHTML=data.runNum;
        cell4.style.cssText='text-align:center;width: 15%;';
        cell4.innerHTML='<span class="spanedit"><a style="cursor: pointer" onclick="addNoticeDialog(1,'+data.id+',\''+data.content+'\',\''+data.url+'\',\''+data.runNum+'\')">修改</a>&nbsp;|&nbsp;<a style="cursor: pointer" onclick="delrecommendDialog('+data.id+')"> 删除</a></span>';

        return row;
    }

    //  系统公告添加
    function addNoticeDialog(type,id,content,url,runNum){

        if(type==0){
            var title='添加';
            var action='add';
        }
        else{
            var title='修改';
            var action='update';
        }
        $('#addNoticeDialog').dialog({
            autoOpen:true,
            width:440,
            height:450,
            title:title,
            resizable:false,
            modal:true,
            open:function(){
                if(type==1){
                    $('#reason').val(content);
                    $('#lianjei').val(url);
                    $('#times').val(runNum);
                }
                else{
                    $('#reason').val('');
                    $('#lianjei').val('');
                    $('#times').val('');
                }
            },
            buttons: {
                "确定": function () {

                    var data={};
                    data.action=action;
                    data.content=$('#reason').val();
                    data.url=$('#lianjei').val();
                    data.status=0;
                    data.runNum=$('#times').val();
                    if($('#reason').val()==''){
                        alert('公告内容不能为空');
                        return false;
                    }
                    if($('#reason').val().length>100){
                        alert('公告内容最大字数为100');
                        return false;
                    }
                    $.ajax({
                        type:"POST",
                        data:data,
                        url:'/ajax/updateAnnouncement/'+id,
                        dataType:'json',
                        success:function(res){
                            if(res.code!=0){
                                alert(res.info);
                            }
                            location.reload();
                        }

                    });

                }
            }
        });
    }

    //  系统公告删除
    function delrecommendDialog(id){

        $('#delrecommendDialog').dialog({
            autoOpen:true,
            width:440,
            height:250,
            title:'删除',
            resizable:false,
            modal:true,
            open:function(){
            },
            buttons: {
                "确定": function () {
                    var data={};
                    data.action='del';
//                    data.uid=uid;
                    $.ajax({
                        type:"POST",
                        data:data,
                        url:'/ajax/updateAnnouncement/'+id,
                        dataType:'json',
                        success:function(res){
                            if(res.code!=0){
                                alert(res.info);
                            }
                            location.reload();
                        }

                    });

                }
            }
        });
    }

    //开启轮播
    function sendAnnouncement(){
        var data={};
        data.count=$('#LCount').val();
        data.rate=$('#LRate').val();
        if($('#LRate').val()<300){
            alert('间隔时间至少为300秒');
            return false;
        }
        if($('#LCount').val()<1){
            alert('轮播持续时长需要大于0');
            return false;
        }
        $.ajax({
            type:"POST",
            data:data,
            url:'/ajax/sendAnnouncement',
            dataType:'json',
            beforeSend: function () {
//               $('.labelP').html('系统消息轮播中.....');
                location.reload();
            },
            success:function(res){
               if(res.code!=0){
                    alert(res.info);
                }
                location.reload();
            },
            complete: function () {
//                $('.labelP').html('系统消息轮播完成');
                location.reload();
            }
        });
    }

    //关闭轮播
    function closeAnnouncement(){

        $.ajax({
            type:"POST",
            url:'/ajax/stopAnnouncement',
            dataType:'json',
//            beforeSend: function () {
//                $('.labelP').html('系统消息轮播中.....');
//            },
            success:function(res){
                if(res.code!=0){
                    alert(res.info);
                }
                location.reload();
            }
//            complete: function () {
//                $('.labelP').html('系统消息轮播完成');
//            }
        });
    }

    function onlyNum() {
        if (!(event.keyCode == 46) && !(event.keyCode == 8) && !(event.keyCode == 37) && !(event.keyCode == 39))
            if (!((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)))
                event.returnValue = false;


        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) { // enter 键
            sendAnnouncement() //要做的事情
        } else {
            return
        }
    }

</script>


